﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSDaoTao : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        //public string TenTruong { get; set; }
        //public string TenChuyenNganh { get; set; }
        public string TenHinhThuc { get; set; }
        //public string TenTrinhDo { get; set; }
        public string Ngay { get; set; }
        public void EditDT(int hdId, string tenTruong, string chuyenNganh, int hinhThucId, string vanBang, DateTime ngayHieuLuc, DateTime ngayHetHieuLuc, string ghiChu)
        {
            List<NSDaoTao> lst = (List<NSDaoTao>)HttpContext.Current.Session["DT"];
            NSDaoTao pc = lst.Where(a => a.DaoTaoId.Equals(hdId)).SingleOrDefault();
            pc.NgayBatDau = ngayHieuLuc;
            pc.NgayKetThuc = ngayHetHieuLuc;
            pc.GhiChu = ghiChu;
            pc.TenTruong = tenTruong;
            pc.ChuyenNganhDaoTao = chuyenNganh;
            pc.HinhThucDaoTaoId = hinhThucId;
            pc.VanBang = vanBang;
            pc.TenHinhThuc = common.LoadCommonCodeById(hinhThucId);
            pc.Ngay = string.Format("{0}-{1}", ngayHieuLuc.ToString("dd/MM/yyyy"), ngayHetHieuLuc.ToString("dd/MM/yyyy"));
        }
        public void DeleteDT(int hdId)
        {
            List<NSDaoTao> lst = (List<NSDaoTao>)HttpContext.Current.Session["DT"];
            NSDaoTao pc = lst.Where(a => a.DaoTaoId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllDTByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSDaoTaos
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSDaoTaos.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateDT_NhanVien(int nhanVienId, List<NSDaoTao> DTObject)
        {

            var queryDTByNhanVienId = (from a in db.NSDaoTaos
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryDTByNhanVienId.Count > 0)
            {
                db.NSDaoTaos.DeleteAllOnSubmit(queryDTByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSDaoTao pc in DTObject)
            {
                NSDaoTao item = new NSDaoTao();
                CopyNSDaoTao(pc, item);
                db.NSDaoTaos.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSDaoTao(NSDaoTao src, NSDaoTao des)
        {
            des.NgayBatDau = src.NgayBatDau;
            des.NgayKetThuc = src.NgayKetThuc;
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.TenTruong = src.TenTruong;
            des.TenTruong = src.TenTruong;
            des.ChuyenNganhDaoTao = src.ChuyenNganhDaoTao;
            des.HinhThucDaoTaoId = src.HinhThucDaoTaoId;
            des.TenHinhThuc = src.TenHinhThuc;
            des.VanBang = src.VanBang;
        }
        public List<NSDaoTao> LoadDTByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSDaoTaos
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.DaoTaoId descending
                         select a).ToList();
            foreach (NSDaoTao item in query)
            {
                //item.TenTruong = common.LoadCommonCodeById(item.TenTruongId);
                //item.TenChuyenNganh = common.LoadCommonCodeById(item.ChuyenNganhDaoTaoId);
                item.TenHinhThuc = common.LoadCommonCodeById(item.HinhThucDaoTaoId);
                //item.TenTrinhDo = common.LoadCommonCodeById(item.TrinhDoDaoTaoId);
                item.Ngay = string.Format("{0}-{1}", item.NgayBatDau.ToString("dd/MM/yyyy"), item.NgayKetThuc.ToString("dd/MM/yyyy"));
            }
            return query;

        }
        public NSDaoTao GetDaoTao(int nhanVienId,string tenTruong, string chuyenNganh, int hinhThucId, string vanBang, DateTime ngayHieuLuc, DateTime ngayHetHieuLuc, string ghiChu)
        {
            var db = new DataClassesDataContext();
            NSDaoTao hd = new NSDaoTao();
            hd.DaoTaoId = 0 - DateTime.Now.Second;
            hd.NgayBatDau = ngayHieuLuc;
            hd.NgayKetThuc = ngayHetHieuLuc;
            hd.GhiChu = ghiChu;
            hd.NhanVienID = nhanVienId;
            hd.TenTruong = tenTruong;
            hd.ChuyenNganhDaoTao = chuyenNganh;
            hd.HinhThucDaoTaoId = hinhThucId;
            hd.VanBang = vanBang;
            hd.TenHinhThuc = common.LoadCommonCodeById(hinhThucId);
            hd.Ngay = string.Format("{0}-{1}", ngayHieuLuc.ToString("dd/MM/yyyy"), ngayHetHieuLuc.ToString("dd/MM/yyyy"));
            return hd;
        }
    }

}